package cn.adkii.springframework.beans.factory.support;

import cn.adkii.springframework.beans.BeansException;
import cn.adkii.springframework.beans.factory.BeanFactory;
import cn.adkii.springframework.beans.factory.config.BeanDefinition;

/**
 * @author malingdi
 * @description 抽象类定义模板方法
 * @date 2023/11/20 14:46
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {


    @Override
    public Object getBean(String beanName) throws BeansException {
        Object bean = getSingleton(beanName);
        if (bean != null){
            return bean;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition);
    }


    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException;
}
